package com.lyh.seckill.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyh.seckill.exception.GlobalException;
import com.lyh.seckill.mapper.UserMapper;
import com.lyh.seckill.pojo.User;
import com.lyh.seckill.service.IUserService;
import com.lyh.seckill.utils.CookieUtil;
import com.lyh.seckill.utils.MD5Util;
import com.lyh.seckill.utils.UUIDUtil;
import com.lyh.seckill.utils.ValidatorUtil;
import com.lyh.seckill.vo.LoginVo;
import com.lyh.seckill.vo.RespBean;
import com.lyh.seckill.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 1.0.0
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements
        IUserService {
    @Autowired
    @Resource
    private UserMapper userMapper;

    @Autowired
    @Resource
    private RedisTemplate redisTemplate;

    //SpringMVC思想 统一异常处理

    /**
     * 登录
     * @param loginVo
     * @return
     */
    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        //根据手机号获取用户
        User user = userMapper.selectById(mobile);
        if (null==user){
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
        //校验密码
        if (!MD5Util.formPassToDBPass(password,user.getSalt()).equals(user.getPassword())){
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
        //生成cookie
        String ticket = UUIDUtil.uuid();
        //为用户请求绑定一个UUID
//        request.getSession().setAttribute(ticket,user);
//       以及用户对象存入Session中

        //将用户信息存入redis中
        redisTemplate.opsForValue().set("user:"+ticket,user);
        //设置cookie的值  并存入
        CookieUtil.setCookie(request,response,"userTicket",ticket);
        return RespBean.success();
    }

    @Override
    public User getUserByCookie(String userTicket,HttpServletRequest request, HttpServletResponse response) {
        if(userTicket.isEmpty()){
            return null;
        }
        //返回用户对象
        User user = (User)redisTemplate.opsForValue().get("user:"+userTicket);
        //用户不为空 重新设置user
        if(user!=null) {
            CookieUtil.setCookie(request,response,"userTicket",userTicket);
        }
        return user;
    }


//    /**
//     * 登录
//     * @param loginVo
//     * @return
//     */
//    @Override
//    public RespBean login(LoginVo loginVo) {
//        String mobile = loginVo.getMobile();
//        String password = loginVo.getPassword();
//        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
//        }
//        if (!ValidatorUtil.isMobile(mobile)){
//            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
//        }
////根据手机号获取用户
//        User user = userMapper.selectById(mobile);
//        if (null==user){
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
//        }
////校验密码
//        if (!MD5Util.formPassToDBPass(password,user.getSalt()).equals(user.getPassword())){
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
//        }
//        return RespBean.success();
//    }



}